package cn.com.shopec.erp.product.commo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class CommodityExcelExport {
	
	private static Logger log = Logger.getLogger(CommodityExcelExport.class);
	/**工作簿**/
	private HSSFWorkbook workbook = null;
	/**工作表**/
	private HSSFSheet sheet = null;
	/**工作行**/
	private HSSFRow row = null;
	/**单元 格**/
	private HSSFCell cell = null;
	private static final int SHEET_MAX_COUNT = 65535;
	
	/**
	 * 创建标题
	 * @param row
	 * @param title
	 */
	@SuppressWarnings("deprecation")
	public void createTitle(HSSFRow row , String [] title)
	{
		try
		{
			cell = row.createCell((short) 0);   
			
			for (int i = 0 ; i < title.length ; i++)
			{
				cell = row.createCell((short) i);   
			    cell.setCellValue(title[i]);   
			}
		}
		catch(Exception e)
		{
			log.error("DataExport writeTitle excpeion:",e);
		}
		
	}
	
	
	/**
	 * 
	 * @param row
	 * @param title
	 */
	public void createWrite(OutputStream out )
	{
		try 
		{
			workbook.write(out);
		} 
		catch (FileNotFoundException e) 
		{
			log.error("DataExport createWrite FileNotFoundException:",e);
		}  
		catch (IOException e) 
		{
			log.error("DataExport createWrite IOException:",e);
		}
		finally
		{
			if(out != null)
			{
				try 
				{
					out.flush();
					out.close();
				} 
				catch (IOException e) 
				{
					log.error("DataExport createWrite IOException:",e);
				}
			}
		}
	
		
	}
	
	
	/**
	 * 创建联系人sheet
	 * @param dataList
	 * @param title
	 * @param out
	 */
	@SuppressWarnings("rawtypes")
	public void createCollectSheet(List list , String [] title , OutputStream out) 
	{
		
	    //默认为1
        int sheetNum = 1;
        int totalSize=list.size()+20;

        //计算要创建的sheet个数
        if(totalSize % SHEET_MAX_COUNT > 0)
        {
            sheetNum = list.size() / SHEET_MAX_COUNT + 1;
        }
        else
        {
            sheetNum = list.size() / SHEET_MAX_COUNT;
        } 
        
       
         workbook = new HSSFWorkbook();  
         
		 /**循环工作表**/
		 for(int i = 0; i < sheetNum ; i++)
		 {
			//i ----------------工作表数量
		    sheet = workbook.createSheet(("sheet"+i)); 
	        row = sheet.createRow(0);   
	           
	        createTitle(row , title);
	        /**循环工作表的第一行**/
	        for(int j = i * SHEET_MAX_COUNT ; j < (i+1) * SHEET_MAX_COUNT ; j++ )
	        {
	        	if(j>totalSize)
	        	{
	        		break;
	        	}
	        	else
	        	{
	        		/**初始化行对象**/
		        	
		        		if(j<list.size())
		        		{
		        			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT));
		        			Object[] obj = (Object[]) list.get(j);
		        			setRowValue(row,obj);
		        		}
		        		else
		        		{
		        			break;
		        		}
		        		    
	        	}
	        	 	
	        	
	        }

		 }
           
        createWrite(out);
	}
	

	private void setRowValue(HSSFRow row2, Object[] obj) {
		String value = null;
		for (int i = 0; i < obj.length; i++) {
			this.cell = this.row.createCell(i);
			switch (i) {
			case 4:
				switch (Integer.valueOf(obj[i].toString())) {
				case 0:
					this.cell.setCellValue("商品");
					break;
				case 1:
					this.cell.setCellValue("赠品");
					break;
				case 2:
					this.cell.setCellValue("免费货品");
					break;
				default:
					break;
				}
				break;
			case 9:
				switch (Integer.valueOf(obj[i].toString())) {
				case 1:
					this.cell.setCellValue("购销");
					break;
				case 2:
					this.cell.setCellValue("代销");
					break;
				default:
					break;
				}
				break;
			case 10:
				switch (Integer.valueOf(obj[i].toString())) {
				case 1:
					this.cell.setCellValue("实库");
					break;
				case 2:
					this.cell.setCellValue("虚库");
					break;
				default:
					break;
				}
				break;
			case 23:
				switch (Integer.valueOf(obj[i].toString())) {
				case 0:
					this.cell.setCellValue("否");
					break;
				case 1:
					this.cell.setCellValue("是");
					break;
				default:
					break;
				}
				break;
			default:
				value = obj[i] == null ? "" : String.valueOf(obj[i]);
				this.cell.setCellValue(value);
				break;
			}
		}
	}
}
